﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NIEN_LUAN
{
    public partial class FormChiTietNguoiDung : Form
    {
        private NIEN_LUAN.Db.DataContentDataContext db = new NIEN_LUAN.Db.DataContentDataContext();
        private Db.NHANVIEN NguoiDungMuonSua;
        private FormQuanLyNguoiDung frmQuanLyNguoiDung;

        public FormChiTietNguoiDung(FormQuanLyNguoiDung frm)
        {
            frmQuanLyNguoiDung = frm;
            InitializeComponent();
        }

        public FormChiTietNguoiDung(Db.NHANVIEN model, FormQuanLyNguoiDung frm)
        {            
            InitializeComponent();
            this.txtHtenNV.Text = model.TenNhanVien;
            this.txtHtenNV.Enabled = false;
            this.txtTenTruyCap.Text = model.TenTruyCap;
            this.txtMatKhau.Text = model.MatKhau;
            NguoiDungMuonSua = model;
            frmQuanLyNguoiDung = frm;
        }


        private void btnThoatCTNgDung_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;//khi click vao day thi tra ve dialog result la cancel
            this.Close();
        }

        private void btnLuuCTNgDung_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtHtenNV.Text.Trim()))
            {
                MessageBox.Show("Tên người dùng rỗng", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (string.IsNullOrEmpty(this.txtTenTruyCap.Text.Trim()))
            {
                MessageBox.Show("Tên truy cập rỗng", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (string.IsNullOrEmpty(this.txtMatKhau.Text.Trim()))
            {
                MessageBox.Show("Mật khẩu rỗng", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }            

            var nguoidung = new NIEN_LUAN.Db.NHANVIEN
            {                
                TenTruyCap = this.txtTenTruyCap.Text,
                TenNhanVien = this.txtHtenNV.Text,               
                MatKhau = this.txtMatKhau.Text,               
            };

            if (frmQuanLyNguoiDung != null && NguoiDungMuonSua == null)
            {
                var nd = (from m in db.NHANVIENs
                          where m.TenTruyCap == this.txtTenTruyCap.Text.Trim()
                          select m).ToList();
                if (nd.Count > 0)
                {
                    MessageBox.Show("Tên truy cập trùng!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                    db.NHANVIENs.InsertOnSubmit(nguoidung);
                    db.SubmitChanges();
                    this.frmQuanLyNguoiDung.NguoiDungAdd = nguoidung;
                    this.DialogResult = DialogResult.OK;                 
            }

            if (frmQuanLyNguoiDung != null && NguoiDungMuonSua != null)
            {
                var _nguoidung = db.NHANVIENs.Single(m => m.TenNhanVien == nguoidung.TenNhanVien);
                _nguoidung.TenTruyCap = nguoidung.TenTruyCap;
                _nguoidung.MatKhau = nguoidung.MatKhau;
                db.SubmitChanges();
                this.frmQuanLyNguoiDung.NguoiDungSua = _nguoidung;
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }
    }
}
